package com.practice.retry;

import org.apache.log4j.Logger;

public abstract class RetryWorker {
	
	private static final Logger logger = Logger.getLogger(RetryWorker.class.getSimpleName());
	
	public static final int FAILED = -1;
	
	private static Integer defaultMaxRetryCount;
	private static Integer defaultSleepMilliseconds;
	
	protected int maxRetryCount = getDefaultMaxRetryCount();
	protected int sleepMilliseconds = getDefaultSleepMilliseconds();
	protected Logger taskLogger = logger;
	
	/**
	 * Initializes and returns default maximal retry count.
	 */
	protected static int getDefaultMaxRetryCount() {
		if (defaultMaxRetryCount == null) {
			defaultMaxRetryCount = 3;
		}
		return defaultMaxRetryCount;
	}
	
	/**
	 * Initializes and returns default thread sleep milliseconds.
	 */
	protected static int getDefaultSleepMilliseconds() {
		if (defaultSleepMilliseconds == null) {
			defaultSleepMilliseconds = 1000;
		}
		return defaultSleepMilliseconds;
	}
	
		
	/**
	 * Gets maximal retry count.
	 */
	public int getMaxRetryCount() {
		return maxRetryCount;
	}

	/**
	 * Sets maximal retry count.
	 */
	public void setMaxRetryCount(int maxRetryCount) {
		this.maxRetryCount = maxRetryCount;
	}

	/**
	 * Gets the value of milliseconds that thread will sleep before retry.
	 */
	public int getSleepMilliseconds() {
		return sleepMilliseconds;
	}

	/**
	 * Sets the value of milliseconds that thread will sleep before retry.
	 */
	public void setSleepMilliseconds(int sleepMilliseconds) {
		this.sleepMilliseconds = sleepMilliseconds;
	}
	
	/**
	 * Gets task related logger.
	 */
	public Logger getTaskLogger() {
		return taskLogger;
	}
	
	/**
	 * Sets task related logger. 
	 * If this attribute is set, log will be output to this logger.
	 * Otherwise, log will be output to default logger.
	 */
	public void setTaskLogger(Logger taskLogger) {
		this.taskLogger= taskLogger;
	}
}
